;------------------------------------------------------------------------
; format conversion 
;------------------------------------------------------------------------

   load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

   filenama = "rnemis.E3.T63.clim.nc" 
   filenamb = "gridarea.nc" 
   filenamc = "T63_jan_surf.nc" 
   filenamo = "rn222_E3.nc"


   fila = addfile(filenama,"r") 
   filb = addfile(filenamb,"r") 
   filc = addfile(filenamc,"r") 

   lat = fila->lat
   lon = fila->lon
   rne = fila->rnemis
   time= fila->time 
   area= filb->cell_area
   oro = filc->SLM

   nlat = dimsizes(lat) 
   nlon = dimsizes(lon) 
   nt   = dimsizes(time) 


   do ilon = 0,nlon-1
      do it = 0,nt-1
         rne(it,:,ilon) = where(lat.ge.-60..and.lat.le.60.,rne(it,:,ilon),-999.)   
      end do 
      area(:,ilon) = where(lat.ge.-60..and.lat.le.60.,area(:,ilon),-999.)   
   end do 

   rne  = where(rne.le.5.E-03,-999.,rne) 
   area = where(rne(0,:,:).le.5.E-03,-999.,area) 

   do it = 0,nt-1
      rne(it,:,:)  = where(oro(:,:).le.5.E-01,-999.,rne(it,:,:)) 
   end do 

   area = where(oro.le.5.E-01,-999.,area) 

   rnsum = rne
   rnsum = 0. 

   total = 0. 

   do im = 0,nt-1
      rnsum(im,:,:) = (/ rne(im,:,:)*area(:,:) /) 
      rnsumx = sum(rnsum(im,:,:)) / sum(area(:,:)) 
      print("rnsum: "+rnsumx) 
      total = rnsumx + total 
   end do 

   total = total / 12. 

   print("total: "+total) 
   system("rm "+filenamo) 

   filo = addfile(filenamo,"c") 

   filo->rnemis=rne

